package dao.mysql;

import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


import dao.ConnPool;
import dao.DAOException;
import dao.HolidayDAO;


public class HolidayDAOImpl implements HolidayDAO
{
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	Statement stmt = null;
	ResultSet result = null;

	public static ArrayList<Date> loadDate(ResultSet result)
	{
		ArrayList<Date> holiList = new ArrayList<Date>();

		try
		{
			while (result.next())
			{
				Date chosen = new Date();

				chosen = result.getDate("Date");

				holiList.add(chosen);
			}

		}
		catch (Exception e)
		{
			System.out.println("Error Occurred at load data for dates");
			System.out.println(e.toString());
			holiList = null;
		}
		return holiList;
	}

	@Override
	public ArrayList<Date> searchHoliByYear(int year) throws DAOException
	{
		// TODO Auto-generated method stub

		ArrayList<Date> holiList = new ArrayList<Date>();

		try
		{
			stmt = ConnPool.GetConnection().createStatement();

			String sql = String.format(
					"Select * from ca_schemata.holidays WHERE Year = %d", year);

			result = stmt.executeQuery(sql);

			holiList = loadDate(result);
		}
		catch (Exception e)
		{
			System.out.println("find all error");
			System.out.println(e.toString());
			holiList = null;
		}

		finally
		{
			ConnPool.closeconnect(stmt, result);
		}

		return holiList;
	}

	@Override
	public ArrayList<Date> searchHoliByYear(int year, int year2)
			throws DAOException
	{
		// TODO Auto-generated method stub
		ArrayList<Date> holiList = new ArrayList<Date>();

		try
		{
			stmt = ConnPool.GetConnection().createStatement();

			String sql = String.format(
					"Select * from ca_schemata.holidays WHERE Year = %d OR Year = %d ", year,year2);

			result = stmt.executeQuery(sql);

			holiList = loadDate(result);
		}
		catch (Exception e)
		{
			System.out.println("find all error");
			System.out.println(e.toString());
			holiList = null;
		}

		finally
		{
			ConnPool.closeconnect(stmt, result);
		}

		return holiList;
	}

}
